<?php

//No direct acesss
defined('_JEXEC') or die();

jimport('joomla.application.component.model');
jimport('joomla.client.helper');

class SimplelistingModelFavourite extends JModel {
        var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_table_prefix = null;

    function __construct()
    {
        parent::__construct();

        global $mainframe, $context;
        $this->_table_prefix = '#__simplelisting';
        $params =&$mainframe->getParams();

        $limit	= $mainframe->getUserStateFromRequest( $context.'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }

    function getData()
    {
        if (empty($this->_data))
        {
                $query = $this->_buildQuery();
                $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_data;
    }

    function getTotal()
    {
        if (empty($this->_total))
        {
                $query = $this->_buildQuery();
                $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }


    function _buildQuery()
    {
        $where = $this->_buildContentWhere();
        $orderby = $this->_buildContentOrderBy();

        $query = 'SELECT f.*,'.
        ' u.name as `username`,'.
        ' p.name as `name`, p.id as `pid`, c.name as `catname`, t.name as `tidname`, p.date_created AS `entrydate`'.
        ' FROM '.$this->_table_prefix.'_favourite as f'.
        ' LEFT JOIN '.$this->_table_prefix.' AS p ON p.id = f.simplelisting_id'.
        ' LEFT JOIN '.$this->_table_prefix.'_categories AS c ON c.id = p.category_id'.
        ' LEFT JOIN '.$this->_table_prefix.'_types AS t ON t.id = p.type_id'.
        ' LEFT JOIN #__users AS u ON u.id = p.create_by' .
        ' LEFT JOIN #__contact_details AS cd ON cd.user_id = p.create_by' .
        $where
        .$orderby;
        return $query;

    }

    function _buildContentWhere()
    {    
        global $mainframe;
        $params =&$mainframe->getParams();
        $user =& JFactory::getuser();
        $where = ' WHERE p.published=1 AND c.published=1 AND t.published=1';

        return $where;
    }

    function _buildContentOrderBy()
    {
        global $mainframe, $context;


        $orderby = ' ORDER BY p.name ASC';

        return $orderby;
    }

    function deleteFav($FIDs)
    {
        $query = "DELETE FROM #__simplelisting_favourite WHERE id IN (".implode(',', $FIDs).")";
        $db = $this->getDBO();
        $db->setQuery($query);
        if (!$db->query()){
            $errorMessage = $this->getDBO()->getErrorMsg();
            JError::raiseError(500, JText::_('ERROR_DELETING_ITEM_MSG').$errorMessage);
        }
    }
    
    function setFav($id, $user_id)
    {
        $query = "INSERT INTO #__simplelisting_favourite
        (`date_created`,`create_by`,`simplelisting_id`)
        VALUES ( NOW(), '.$user_id.', '.$id.' )";
        $db = $this->getDBO();
        $db->setQuery($query);
        if (!$db->query()){
            $errorMessage = $this->getDBO()->getErrorMsg();
            JError::raiseError(500, JText::_('ERROR_ADDING_FAV_MSG').$errorMessage);
        }
    }
}
?>
